1. Field of the Invention
Embodiments of the present invention generally relate to computer programming and more specifically to methods for providing vector data types that support swizzling and write masking operations in an object-oriented programming language.
2. Description of the Related Art
Graphics hardware includes graphics processing units (GPUs). GPUs provide a processor capable of executing instructions that generate images produced on a display device. This includes performing calculating values related to lighting effects, object transformations and 3D motion. Shading language programs may be developed and compiled for execution on the GPU. Because many graphics related operations are very vector intensive, the hardware instructions generated from a shading language program may be heavily optimized for vector processing.
Additionally, GPU shading languages such as Cg, HLSL, and the OpenGL Shading Language (GLSL) each include built in support for vector data types. These shading languages also provide a simple and elegant syntax for vector operations referred to as “swizzling” and “write masking.” Swizzling allows a developer of a shading language program to specify an ordering of some (or all) of the components of a vector to use in a particular operation. For example, consider the vector <1, 2, 3, 4>, a “swizzle” of this vector may reorder the elements of this vector as <4, 3, 2, 1>, <4, 4, 4, 4>, <4, 2>, <1> or any other combination. In shading languages, a vector may be swizzled by suffixing a vector variable with a swizzle pattern. The swizzle pattern uses a different character to specify each component of a vector. For example, the swizzle patterns for the vectors listed above are “xyzw” for the unswizzled vector and “wzyx,” “wwww,” “wy,” and “x,” respectively for the swizzled ones. When a swizzle pattern suffixes a vector being assigned values, the swizzle pattern operates as a write mask. A write mask specifies which vector components an assignment operation should write to. For example, the write mask of “wzyx” specifies to assign values to a vector in reverse order. As another example, the write mask “yz” specifies to assign values to the y and z components of a vector, leaving other values unchanged.
The common syntax adopted by Cg, HLSL, and GLSL for swizzling and write masking operations have proven to be both very succinct and very powerful for vector manipulation. Accordingly, providing support for the syntax of swizzling and write masking operations for other programming languages would extend the usefulness of these languages. However, most object-oriented languages, including C++, lack a built in vector data type. Such types may be declared as a class, and the C++ standard template library includes a vector type. The vector data types provided by these mechanisms lack support for the shading language syntax for swizzling and write masking operations. Further, the syntax of a vector swizzle (i.e., foo.xxy) conflicts with the syntax for class or structure member accesses in C++(and other object-oriented programming languages). That is, the vector swizzle of “foo.xxy” appears to a C++ compiler as an attempt to access a member variable “xxy” of the object “foo.” Similarly, when used as the left value for an assignment operation, the write mask “foo.xz” appears to the C++ compiler as an assignment to the member variable “xy” of the object “foo.”
One approach to supporting swizzle and write mask vector operations is to modify a compiler for an object-oriented language to include a vector data type which supports swizzling and write masking. In most cases, this is not possible as users are unable to modify most commercially available compilers. Furthermore, this approach requires each compiler be modified individually.
Also, one difficulty faced by the developer of shading language programs is debugging. Because shading language programs are executed on the GPU, the output of a given program may be observed only indirectly by viewing the images generated by the shading language program. In contrast, object-oriented programming languages, such as C++, have a wide variety of development and debugging tools available. Thus, it would be useful to allow shading language programs to be developed and tested using these tools. However, as described above the vector data types available to the C++ programmer are not interoperable with the shading language vector data types.
As the foregoing illustrates, what is needed in the art is a vector data type for object-oriented programming languages, such as C++, that supports swizzling and write masking operations.